using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Editor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = ConfigurationManager.AppSettings["PageTitle"] + " - Editor";
        if (Session["SessionID"] != null)
        {
            Hidden_SessionID.Value = Convert.ToString(Session["SessionID"]);
        }
        else
        {
            Response.Redirect("ErrorPage.aspx");
        }
        if (Session["UserID"] != null)
        {
            Hidden_UserID.Value = Convert.ToString(Session["UserID"]);
        }
        else
        {
            Response.Redirect("ErrorPage.aspx");
        }
        
        ListBox_SelectedDates.Style.Add("display", "none");

        // Set up user hidden user access list box for access from client side
        ListBox_UserAccess.Style.Add("display", "none");
        DataTable UserAccessDT = new DataTable();
        if (Session["UserAccess"] != null)
        {
            UserAccessDT = (DataTable)Session["UserAccess"];
        }
        else
        {
            Response.Redirect("ErrorPage.aspx");
        }
        ListBox_UserAccess.DataSource = UserAccessDT;
        ListBox_UserAccess.DataTextField = UserAccessDT.Columns[0].ColumnName;
        ListBox_UserAccess.DataValueField = UserAccessDT.Columns[0].ColumnName;
        ListBox_UserAccess.DataBind();

        if (!Page.IsPostBack)
        {
            if (PreviousPage != null)
            {
                if (PreviousPage.Title.Contains("Schedule List"))
                {
                    string EditorHeader = "";
                    string ListLookupIDs = "";
                    ListBox SelectedDates = (ListBox)PreviousPage.Form.FindControl("ListBox_Selected");
                    if (SelectedDates != null)
                    {
                        HiddenField BtnChoice = (HiddenField)PreviousPage.Form.FindControl("Hidden_BtnChoice");
                        Hidden_PreviousBtnChoice.Value = BtnChoice.Value;
                        if (BtnChoice.Value == "EditWeek")
                        {
                            //string HeaderStr = "";
                            string weekStart = Convert.ToDateTime(SelectedDates.Items[0].Value).ToString("M/dd/yy");
                            string weekEnd = Convert.ToDateTime(SelectedDates.Items[6].Value).ToString("M/dd/yy");
                            foreach (ListItem Item in SelectedDates.Items)
                            {
                                DateTime dt = Convert.ToDateTime(Item.Value);
                                EditorHeader += dt.ToString("ddd") + " " + dt.ToString("M/d") + "_";
                                ListLookupIDs += dt.ToString("M/dd/yyyy") + "_";
                                //HeaderStr += dt.ToString("ddd") + " " + dt.ToString("M/d") + ", ";
                            }
                            Lbl_ScheduleHeader.Text = "Schedules: Week of " + weekStart + " - " + weekEnd;
                        }
                        else if (BtnChoice.Value == "EditAll")
                        {
                            string HeaderStr = "";
                            foreach (ListItem Item in SelectedDates.Items)
                            {
                                DateTime dt = Convert.ToDateTime(Item.Value);
                                EditorHeader += dt.ToString("ddd") + " " + dt.ToString("M/d") + "_";
                                ListLookupIDs += dt.ToString("M/dd/yyyy") + "_";
                                HeaderStr += dt.ToString("ddd") + " " + dt.ToString("M/d") + ", ";
                            }
                            Lbl_ScheduleHeader.Text = "Schedules: " + HeaderStr.Remove(HeaderStr.Length - 2);
                        }
                        else
                        {
                            DateTime dt = Convert.ToDateTime(SelectedDates.SelectedValue);
                            EditorHeader += dt.ToString("ddd") + " " + dt.ToString("M/d") + "_";
                            ListLookupIDs += dt.ToString("M/dd/yyyy") + "_";
                            Lbl_ScheduleHeader.Text = "Schedule: " + dt.ToString("ddd") + " " + dt.ToString("M/d");
                        }
                    }
                    Hidden_EditorHeader.Value = EditorHeader.Remove(EditorHeader.Length - 1);
                    Hidden_ListLookupIDs.Value = ListLookupIDs.Remove(ListLookupIDs.Length - 1);
                    Hidden_EditorPageID.Value = "scheduleEditor";
                }
                if (PreviousPage.Title.Contains("Package List"))
                {
                    string ListLookupIDs = "";
                    ListBox SelectedPackages = (ListBox)PreviousPage.Form.FindControl("ListBox_Selected");
                    if (SelectedPackages != null)
                    {
                        HiddenField BtnChoice = (HiddenField)PreviousPage.Form.FindControl("Hidden_BtnChoice");
                        Hidden_PreviousBtnChoice.Value = BtnChoice.Value;
                        if (BtnChoice.Value == "EditAll")
                        {
                            foreach (ListItem Item in SelectedPackages.Items)
                            {
                                ListLookupIDs += Item.Value + "_";
                            }
                        }
                        else
                        {
                            ListLookupIDs += SelectedPackages.SelectedValue + "_";
                        }
                    }
                    Hidden_EditorPageID.Value = "packageEditor";
                    Hidden_ListLookupIDs.Value = ListLookupIDs.Remove(ListLookupIDs.Length - 1);
                    //Hidden_ListLookupIDs.Value = "1";
                }
                if (PreviousPage.Title.Contains("Event List"))
                {
                    //ListBox SelectedEvent = (ListBox)PreviousPage.Form.FindControl("ListBox_Results");
                    //Hidden_EditorPageID.Value = "eventEditor";
                    //Hidden_ListLookupIDs.Value = SelectedEvent.SelectedItem.Value;

                    Hidden_EditorPageID.Value = "eventEditor";
                    HiddenField BtnChoice = (HiddenField)PreviousPage.Form.FindControl("Hidden_BtnChoice");
                    Hidden_PreviousBtnChoice.Value = BtnChoice.Value;
                    if (BtnChoice.Value != "New")  // open event editor with chosen event
                    {
                        ListBox SelectedEvent = (ListBox)PreviousPage.Form.FindControl("ListBox_Results");
                        Hidden_ListLookupIDs.Value = SelectedEvent.SelectedItem.Value;
                    }
                }
                if (PreviousPage.Title.Contains("Combo List"))
                {
                    Hidden_EditorPageID.Value = "comboEditor";
                    HiddenField BtnChoice = (HiddenField)PreviousPage.Form.FindControl("Hidden_BtnChoice");
                    Hidden_PreviousBtnChoice.Value = BtnChoice.Value;
                    if (BtnChoice.Value == "Edit")  // open combo editor with chosen combo
                    {
                        ListBox SelectedCombo = (ListBox)PreviousPage.Form.FindControl("ListBox_Results");
                        Hidden_ListLookupIDs.Value = SelectedCombo.SelectedItem.Value;
                    }
                    else if (BtnChoice.Value == "EditDefault")
                    {
                        DropDownList SelectedCombo = (DropDownList)PreviousPage.Form.FindControl("DropDown_DefaultCombo");
                        Hidden_ListLookupIDs.Value = SelectedCombo.SelectedItem.Value;
                    }
                }
                if (PreviousPage.Title.Contains("Asset List"))
                {
                    Hidden_EditorPageID.Value = "assetEditor";
                    HiddenField BtnChoice = (HiddenField)PreviousPage.Form.FindControl("Hidden_BtnChoice");
                    Hidden_PreviousBtnChoice.Value = BtnChoice.Value;
                    if (BtnChoice.Value != "New")  // open asset editor with chosen asset
                    {
                        ListBox SelectedAsset = (ListBox)PreviousPage.Form.FindControl("ListBox_Results");
                        Hidden_ListLookupIDs.Value = SelectedAsset.SelectedItem.Value;
                    }
                }
            }
        }
    }
    protected void Calendar_CopyWeek_SelectionChanged(object sender, EventArgs e)
    {
        if (Calendar_CopyWeek.SelectedDates.Count == 7)
        {
            ListBox_SelectedDates.Items.Clear();
            foreach (DateTime dt in Calendar_CopyWeek.SelectedDates)
            {
                string strDate = dt.ToString("M/dd/yy");
                ListBox_SelectedDates.Items.Add(strDate);
            }
            if (ListBox_SelectedDates.SelectedIndex > 0)
            {
                ListBox_SelectedDates.SelectedIndex = ListBox_SelectedDates.SelectedIndex - 1;
            }
            else
            {
                ListBox_SelectedDates.SelectedIndex = ListBox_SelectedDates.Items.Count - 1;
            }
        }
        else
        {
            ListBox_SelectedDates.Items.Clear();
        }
    }
    protected void Calendar_CopyWeek_MonthChanged(object sender, MonthChangedEventArgs e)
    {
        ListBox_SelectedDates.Items.Clear();
    }
}